import 'package:flutter/material.dart';
import 'package:hockey/jzx_apps.dart';
import 'package:hockey/services/jzx_config.dart';

const _kReportReason = [
  'Pornographic',
  'False gender',
  'Fraud',
  'Political sensitive',
  'Other',
  'Cancel'
];

class JzxOptionsSheet extends StatelessWidget {
  const JzxOptionsSheet(
    this.uid, {
    super.key,
    this.onReport,
    this.onFollow,
    this.onBlock,
  });
  final String uid;

  final VoidCallback? onReport;
  final VoidCallback? onFollow;
  final VoidCallback? onBlock;

  @override
  Widget build(BuildContext context) {
    var options = ['Follow', 'Block', 'Report', 'Cancel'];
    if (JzxConfigService.to.isFollow(uid)) {
      options[0] = 'Unfollow';
    }

    return CommonSheetView(options, (e) async {
      Get.back();
      if (e == 'Report') {
        await Get.bottomSheet(
          JzxReportSheet(uid),
          backgroundColor: Colors.white,
          isScrollControlled: true,
          barrierColor: myAppColors.barrierColor,
        );
        onReport?.call();
        return;
      }

      if (e == 'Follow') {
        showLoading();
        await JzxNetRepository.client.addFollow({'followUserId': uid});
        hideLoading();
        JzxConfigService.to.updateFollows(uid);
        showMessage('Follow Successfully');
        onFollow?.call();
        return;
      }

      if (e == 'Unfollow') {
        showLoading();
        await JzxNetRepository.client.removeFollow({'followUserId': uid});
        hideLoading();
        JzxConfigService.to.updateFollows(uid);
        showMessage('Unfollow Successfully');
        onFollow?.call();
        return;
      }

      if (e == 'Block') {
        showLoading();
        await JzxNetRepository.client.addBlock({
          'broadcasterId': uid,
          'complainCategory': 'Block',
        });
        JzxLogPvManger.tracking(page: JzxLogPages.block);
        hideLoading();
        JzxConfigService.to.updateBlocks(uid);


        showMessage('Block Successfully');
        onBlock?.call();
        return;
      }
    });
  }
}

class JzxReportSheet extends StatelessWidget {
  const JzxReportSheet(this.uid, {super.key});

  final String uid;

  @override
  Widget build(BuildContext context) {
    return CommonSheetView(_kReportReason, (e) {
      Get.back();

      showLoading();
      JzxNetRepository.client.addBlock({
        'broadcasterId': uid,
        'complainCategory': 'Report',
        'complainSub': e,
      });

      JzxLogPvManger.tracking(page: JzxLogPages.report, subPage: e);
      hideLoading();
      showMessage('Report Successfully');
    });
  }
}

class CommonSheetView extends StatelessWidget {
  const CommonSheetView(this.items, this.onTap, {super.key});

  final List<String> items;
  final ValueChanged<String> onTap;

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(
          bottom: Get.bottomBarHeight > 0 ? 20 : 0,
        ),
        child: Column(
          children: [
            for (final e in items)
              InkWell(
                onTap: () {
                  if (e == 'Cancel') {
                    Get.back();
                    return;
                  }
                  onTap.call(e);
                },
                child: Container(
                  height: 56.w,
                  width: double.infinity,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border(
                      top: BorderSide(
                        width: e == 'Cancel' ? 8 : 1,
                        color: e == 'Cancel'
                            ? myAppColors.background
                            : myAppColors.border,
                      ),
                    ),
                  ),
                  child: Text(
                    e,
                    style: myAppColors.black.font_17,
                  ),
                ),
              )
          ],
        ),
      ),
    );
  }
}
